package ad_exercise_3.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

import ad_exercise_3.model.Card.CardColour;

/**
 * Utility class for asking a user for input. Input is checked for validity using given options.
 * 
 * @author Karen Berntsen (s0815799)
 * @author Erik Stens (s0615374)
 */
public class GameHelper {
	public static String getUserInput(String prompt, String... options) {
		System.out.println(prompt);
		
		String userInput = new String();
		while (userInput.length() == 0) {
			Scanner userInputScanner = new Scanner(System.in);
			userInput = userInputScanner.nextLine();
			
			boolean isOption = false;
			for (String option : options) {
				if (option.equalsIgnoreCase(userInput)) {
					isOption = true;
					break;
				}
			}
			
			if (!isOption && userInput.length() != 0) {
				System.out.println("Invalid input, please use any of these options: " + Arrays.toString(options));
				userInput = "";
			}
		}
		return userInput;
	}
	
	public static String getUserInput(String prompt, Object... options) {
		String[] stringOptions = new String[options.length];
		for (int i = 0; i < options.length; i++) {
			Object option = options[i];
			if (option instanceof String) {
				stringOptions[i] = (String) option;
			} else if (option instanceof List<?>) {
				return getUserInput(prompt, ((List<?>) option).toArray());
			} else if (option instanceof Integer) {
				stringOptions[i] = ((Integer) option).toString();
			} else if (option instanceof CardColour) {
				stringOptions[i] = ((CardColour) option).toString();
			} else {
				throw new RuntimeException("Argument type not supported!");
			}
		}
		
		return getUserInput(prompt, stringOptions);
	}
	
	public static String getUserInput(String prompt, List<Object> options) {
		return getUserInput(prompt, options.toArray());
	}
}
